import SDKManager from "../../sdk/SDKManager";
import { ResultState, BattleID } from "../../sdk/SDKConfig";
import BagManager from "../bag/BagManager";
import { InitValue } from "../../public/Config";
import RandomHelper from "../../cfw/tools/RandomHelper";
import TipController from "../tip/TipController";
import BigExportC from "../../sdk/appBoxAd/BigExportC";
import BaseView from "../../cfw/mvc/BaseView";
let targetPosX: number[] = [-780, 0, 776]
let targetPosY: number = -640
let startPos: number = 831
const { ccclass, property } = cc._decorator;

@ccclass
export default class FloatView extends BaseView {

    @property
    animType: number = 0;

    start() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.startAnimation();
    }

    startAnimation() {
        if (!cc.isValid(this.node)) {
            return;
        }
        cc.Tween.stopAllByTarget(this.node)
        let time = RandomHelper.random(10, 20)
        let stayTime = RandomHelper.random(5, 10)
        let t = cc.tween;
        this.node.x = 0
        // this.node.active = true;
        this.node.opacity = 255;
        this.node.y = startPos;
        this.node.angle = 0;
        let tx = targetPosX[RandomHelper.random(0, targetPosX.length)];
        t(this.node).repeatForever(cc.tween(this.node).by(1, { angle: 10 }).by(1, { angle: -10 })).start()
        t(this.node).to(time, { x: tx, y: targetPosY }).to(stayTime, {}).call(() => {
            this.startAnimation()
            // console.log('All tweens finished.')
        }).start()



    }

    touchStart() {
        if (this.node.opacity < 0) {
            return;
        }
        this.node.opacity = 0;
        // this.node.active = false;
        // if (this.animType == 0) {
        //     if (SDKManager.getChannel().hasBigExport()) {
        //         BigExportC.instance().intoExport2()
        //     } else {
        //         BagManager.buyCoin()
        //     }

        // } else {
        SDKManager.getChannel().battleStart(BattleID.CLICK_PADA_RETURN_GAME)
        if (SDKManager.getChannel().isStartErrorClick()) {
            SDKManager.getChannel().showRewardAd(0, (state: ResultState) => {
                SDKManager.getChannel().battleEnd(BattleID.CLICK_PADA_RETURN_GAME)
                if (state == ResultState.YES) {
                    if (cc.isValid(this.node)) {
                        let pos = this.node.parent.convertToWorldSpaceAR(this.node.getPosition())
                        BagManager.addCoin(InitValue.AD_BUY_GOLD, pos)
                    } else {
                        BagManager.addCoin(InitValue.AD_BUY_GOLD)
                    }
                }
            })
        } else {
            BagManager.buyCoin(() => {
                SDKManager.getChannel().battleEnd(BattleID.CLICK_PADA_RETURN_GAME)
            })
        }
        // }
        this.setTimeout(() => {
            this.startAnimation()
        }, 5000);
        // cc.tween(this.node).to(5, {}).call(() => {

        // }).start()
    }

}
